package com.modules.timer.job;

import com.alibaba.fastjson.JSONObject;
import com.common.util.SpringContextUtil;
import com.modules.timer.entity.TimerTaskEntity;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.lang.reflect.Method;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TimerJob extends QuartzJobBean {

    private ExecutorService service = Executors.newSingleThreadExecutor();
    private Logger logger = LoggerFactory.getLogger(getClass());


    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        TimerTaskEntity scheduleJob = (TimerTaskEntity) jobExecutionContext.getMergedJobDataMap()
                .get("JOB_PARAM_KEY");
        logger.info("定时任务开始执行,参数："+JSONObject.toJSONString(scheduleJob));

        String params = null;
        try {
            Object target = SpringContextUtil.getBean(scheduleJob.getBeanName());
            Method method = target.getClass().getDeclaredMethod(scheduleJob.getMethodName(), String.class);
            Object result = method.invoke(target, scheduleJob.getParams());
            logger.info("定时任务执行成功，返回值"+JSONObject.toJSONString(result));
        }catch (Exception e){
            logger.error("定时任务执行异常：",e);
        }
    }
}
